---
--类名: PhotoMenuRender.lua
--创建日期: 2018-7-30
--创建人: 宋建邦
--功能描述:菜单
--修改内容:
--修改日期:
--修改人:
--修改备注:

local Res = {
    PhotoMenuNode = "ui2/main/photo_more.csb",
    PhotoMenuItemNode = "ui2/main/photo_more_node.csb",
}

local function posadd(c, x, y)
    c:setPosition(cc.p(c:getPositionX() + x, c:getPositionY() + y))
end

local PhotoMenuRender = app.mvc.createRenderClass(...)

-- arg: {
--      name = "",
--      itemInit = function,
--      items = {{id = id, path = "icon_path", name = "name", cb_select = function},
--      {id = id, path = "icon_path", name = "name", cb_select = function},
--      ...
--      }},
function PhotoMenuRender.createWithData(data, node)
    local sprite = PhotoMenuRender.extend(node or cc.CSLoader:createNode(Res.PhotoMenuNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end

function PhotoMenuRender.create(node)
    return PhotoMenuRender.createWithData(nil, node)
end

function PhotoMenuRender:getMenuItem(itemd, t)
    local itm = cc.CSLoader:createNode(Res.PhotoMenuItemNode)
    if self._data.itemInit then
        self._data.itemInit(itm, itemd, t)
    end

    return itm
end

function PhotoMenuRender:init(data)
    self.super.init(self, data)

    self:setContentSize(self._ui["Panel"]:getContentSize())
    self._ui.Panel:setBackGroundColorType(1)
    
    self.scrollView = self:findChild("Panel/ScrollView")

    self.scrollView:setBounceEnabled(true)

    local menuItems = data.items
    self._ui["Panel/name"]:setString(data.name)
    self:refreshMenu(menuItems)
    self:refreshScrollView(menuItems)
    self:refresh()

    self:setNodeEventEnabled(true)
    local pc = app:getInst("PhotoController")    
    EM:on(pc.evtConfigChange, function()
        self:refreshScrollView(menuItems)    
    end)
end

function PhotoMenuRender:onExit()
    local pc = app:getInst("PhotoController")
    EM:removeByName(pc.evtConfigChange)
end

function PhotoMenuRender:refreshMenu(menuItems)
    local iw = 92
    local ih = 92

    local mc = #menuItems
    local lc = math.ceil(mc/2)
    local offh = ih * (lc - 1)

    if lc > 1 then
        local osz = self:getContentSize()
        local nsz = cc.size(osz.width, osz.height + offh)

        self:findChild("Panel"):setContentSize(nsz)
        self:setContentSize(nsz)
        self:findChild("Panel/bg"):setContentSize(nsz)

        local ossize = self.scrollView:getContentSize()
        local nssize = cc.size(ossize.width, ossize.height + offh)
        self.scrollView:setContentSize(nssize)
        
        posadd(self._ui["Panel/bg"], 0, offh)
        posadd(self._ui["Panel/ScrollView"], 0, offh)
        posadd(self._ui["Panel/name"], 0, offh)

        self._ui["Panel/arrow"]:setPosition(cc.p(nsz.width, nsz.height * 0.5))
    else
        local osz = self:getContentSize()
        local nsz = cc.size(osz.width, osz.height + offh)
        self._ui["Panel/arrow"]:setPosition(cc.p(nsz.width, nsz.height * 0.5))
    end
end

function PhotoMenuRender:refreshScrollView(menuItems)
    self.scrollView:removeAllChildren()
    local function addItem(item, pos)
        self.scrollView:addChild(item, 0)
        item:setPosition(pos)
    end

    local menuCount = #menuItems
    local iw = 92
    local ih = 92
    local innerSize = cc.size(iw*2+10, ih*(math.ceil(menuCount/2)))

    local bx = 0
    local by = 0
    
    for i = 1, #menuItems do
        addItem(self:getMenuItem(menuItems[i], self._data.type),
        cc.p(bx + math.fmod(i + 1, 2) * iw, innerSize.height - (by + ih * math.floor((i+1)/2))),
        menuItems[i])
    end

    self.scrollView:setInnerContainerSize(innerSize)
end

function PhotoMenuRender:refresh()
    if not self._data then return end
end

function PhotoMenuRender:linkTo(parent)
    local x = parent:getPositionX()
    local y = parent:getPositionY()

    local ps = parent:getContentSize()

    local wp = parent:getParent():convertToWorldSpace(cc.p(parent:getPositionX(), parent:getPositionY()))
    wp.x = wp.x - ps.width
    wp.y = wp.y + ps.height * 0.5
    
    self:setTargetPosition(wp)
end

function PhotoMenuRender:setTargetPosition(worldPos)
    local selfPosition = self:getParent():convertToNodeSpace(worldPos)
    local ssz = self:getContentSize()

    if selfPosition.y + ssz.height * 0.5 > display.height then
        posadd(self._ui["Panel/arrow"], 0, (selfPosition.y + ssz.height * 0.5 - display.height))
        selfPosition.y = display.height - ssz.height * 0.5        
    elseif selfPosition.y - ssz.height * 0.5 < 0 then
        posadd(self._ui["Panel/arrow"], 0, selfPosition.y - ssz.height * 0.5)
        selfPosition.y = ssz.height * 0.5        
    end
    self:setPosition(selfPosition)
end

return PhotoMenuRender
